package com.ruoyi.cust.controller;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/users")
public class UserController {

    // 模拟数据库存储用户信息
    private static final Map<Long, User> users = new HashMap<>();

    static {
        // 初始化一些测试数据
        users.put(1L, new User(1L, "张三", 25, "zhangsan@example.com"));
        users.put(2L, new User(2L, "李四", 30, "lisi@example.com"));
    }

    // 获取所有用户
    @Anonymous
    @GetMapping
    public List<User> getAllUsers() {
        return new ArrayList<>(users.values());
    }

    // 根据ID获取用户
    @Anonymous
    @GetMapping("/{id}")
    public ResponseEntity<User> getUserById(@PathVariable Long id) {
        User user = users.get(id);
        if (user == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(user);
    }

    // 创建新用户
    @Anonymous
    @PostMapping
    public ResponseEntity<User> createUser(@RequestBody User user) {
        Long newId = (long) (users.size() + 1);
        user.setId(newId);
        users.put(newId, user);
        return new ResponseEntity<>(user, HttpStatus.CREATED);
    }

    // 更新用户信息
    @Anonymous
    @PutMapping("/{id}")
    public ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody User user) {
        if (!users.containsKey(id)) {
            return ResponseEntity.notFound().build();
        }
        user.setId(id);
        users.put(id, user);
        return ResponseEntity.ok(user);
    }

    // 删除用户
    @Anonymous
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
        if (!users.containsKey(id)) {
            return ResponseEntity.notFound().build();
        }
        users.remove(id);
        return ResponseEntity.noContent().build();
    }

    // 用户实体类
    public static class User {
        private Long id;
        private String name;
        private int age;
        private String email;

        // 无参构造函数，用于JSON反序列化
        public User() {}

        public User(Long id, String name, int age, String email) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.email = email;
        }

        // Getter和Setter方法
        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }
    }
}
